<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce">
							<el-col v-if="$check_field('get','promotion_name') || $check_field('add','promotion_name') || $check_field('set','promotion_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宣传名称" prop="promotion_name">
												<el-input id="promotion_name" v-model="form['promotion_name']" placeholder="请输入宣传名称"
							  v-if="(form['publicity_and_education_id'] && $check_field('set','promotion_name')) || (!form['publicity_and_education_id'] && $check_field('add','promotion_name'))" :disabled="disabledObj['promotion_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','promotion_name')">{{form['promotion_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','promotion_type') || $check_field('add','promotion_type') || $check_field('set','promotion_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宣传类型" prop="promotion_type">
												<el-input id="promotion_type" v-model="form['promotion_type']" placeholder="请输入宣传类型"
							  v-if="(form['publicity_and_education_id'] && $check_field('set','promotion_type')) || (!form['publicity_and_education_id'] && $check_field('add','promotion_type'))" :disabled="disabledObj['promotion_type_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','promotion_type')">{{form['promotion_type']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','promotional_video') || $check_field('add','promotional_video') || $check_field('set','promotional_video')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宣传视频" prop="promotional_video">
												<el-upload v-if="(form['publicity_and_education_id'] && $check_field('set','promotional_video')) || (!form['publicity_and_education_id'] && $check_field('add','promotional_video'))" class="upload-demo" drag
						action="" style="max-width: 300px;width: 100%;" :http-request="upload_promotional_video" :limit="1" accept="video/ogg,video/mp4,video/webm">
						<i class="el-icon-upload"></i>
						<div class="el-upload__text">将视频拖到此处，或<em>点击上传</em></div>
					</el-upload>
					<div v-else-if="$check_field('get','promotional_video')">
						<el-button type="primary" @click="download(form['promotional_video'])">下载<i
								class="el-icon-download el-icon--right"></i></el-button>
					</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','propaganda_objective') || $check_field('add','propaganda_objective') || $check_field('set','propaganda_objective')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宣传目的" prop="propaganda_objective">
												<el-input id="propaganda_objective" v-model="form['propaganda_objective']" placeholder="请输入宣传目的"
							  v-if="(form['publicity_and_education_id'] && $check_field('set','propaganda_objective')) || (!form['publicity_and_education_id'] && $check_field('add','propaganda_objective'))" :disabled="disabledObj['propaganda_objective_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','propaganda_objective')">{{form['propaganda_objective']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','teacher_users') || $check_field('add','teacher_users') || $check_field('set','teacher_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="教师用户" prop="teacher_users">
																		<div v-if="user_group !== '管理员'">
							{{ get_user_session_teacher_users(form['teacher_users']) }}
							<el-select v-if="(form['publicity_and_education_id'] && $check_field('set','teacher_users')) || (!form['publicity_and_education_id'] && $check_field('add','teacher_users'))" id="teacher_users" v-model="form['teacher_users']" :disabled="disabledObj['teacher_users_isDisabled']">
								<el-option v-for="o in list_user_teacher_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','teacher_users')" id="teacher_users" v-model="form['teacher_users']" :disabled="true">
								<el-option v-for="o in list_user_teacher_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="teacher_users" v-model="form['teacher_users']" :disabled="disabledObj['teacher_users_isDisabled']">
							<el-option v-for="o in list_user_teacher_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','promotional_cover') || $check_field('add','promotional_cover') || $check_field('set','promotional_cover')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宣传封面" prop="promotional_cover">
								<el-upload :disabled="disabledObj['promotional_cover_isDisabled']" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_promotional_cover"
						:show-file-list="false" v-if="(form['publicity_and_education_id'] && $check_field('set','promotional_cover')) || (!form['publicity_and_education_id'] && $check_field('add','promotional_cover'))">
						<img id="promotional_cover" v-if="form['promotional_cover']" :src="$fullUrl(form['promotional_cover'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','promotional_cover')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['promotional_cover'])" :preview-src-list="[$fullUrl(form['promotional_cover'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','promotional_content') || $check_field('add','promotional_content') || $check_field('set','promotional_content')" :xs="24" :sm="24" :lg="24" class="el_form_editor_warp">
				<el-form-item label="宣传内容" prop="promotional_content">
					<quill-editor v-model.number="form['promotional_content']"
						v-if="(form['publicity_and_education_id'] && $check_field('set','promotional_content')) || (!form['publicity_and_education_id'] && $check_field('add','promotional_content')) ">
					</quill-editor>
					<div v-else-if="$check_field('get','promotional_content')" v-html="form['promotional_content']"></div>
				</el-form-item>
			</el-col>
								<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="审核状态" prop="examine_state">
					<el-select id="examine_state" v-model="form['examine_state']"
						v-if="(form['examine_state'] && $check_examine()) || (!form['examine_state'] && $check_examine())" :disabled="true">
						<el-option key="未审核" label="未审核" value="未审核"></el-option>
						<el-option key="已通过" label="已通过" value="已通过"></el-option>
						<el-option key="未通过" label="未通过" value="未通过"></el-option>
					</el-select>
					<div v-else>{{form["examine_state"]}}</div>
				</el-form-item>
			</el-col>
		
	
	
		
		
	
	
	
	
	</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/publicity_and_education/view','set') || $check_action('/publicity_and_education/view','add') || $check_option('/publicity_and_education/table','examine')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "publicity_and_education_id",
				url_add: "~/api/publicity_and_education/add?",
				url_set: "~/api/publicity_and_education/set?",
				url_get_obj: "~/api/publicity_and_education/get_obj?",
				url_upload: "~/api/publicity_and_education/upload?",

				query: {
					"publicity_and_education_id": 0,
				},

				form: {
								"promotion_name":  '', // 宣传名称
										"promotion_type":  '', // 宣传类型
										"promotional_video":  '', // 宣传视频
										"propaganda_objective":  '', // 宣传目的
										"teacher_users": 0, // 教师用户
										"promotional_cover":  '', // 宣传封面
										"promotional_content":  '', // 宣传内容
									"examine_state": "未审核",
								"publicity_and_education_id": 0, // ID
													},
				disabledObj:{
								"promotion_name_isDisabled": false,
										"promotion_type_isDisabled": false,
										"promotional_video_isDisabled": false,
										"propaganda_objective_isDisabled": false,
										"teacher_users_isDisabled": false,
										"promotional_cover_isDisabled": false,
										"promotional_content_isDisabled": false,
										},

	
				
				
				
				
					// 用户列表
				list_user_teacher_users: [],
						// 用户组
				group_user_teacher_users: "",
						
				
			
			}
		},
		methods: {

	
	
			
	
						/**
			 * 上传宣传视频
			 * @param {Object} param 视频参数
			 */
			upload_promotional_video(param){
									this.uploadFile(param.file, "promotional_video");
								},
	
	
			
	
			
	
				/**
			 * 获取教师用户用户列表
			 */
			async get_list_user_teacher_users() {
                var json = await this.$get("~/api/user/get_list?user_group=教师用户");
                if(json.result && json.result.list){
                    this.list_user_teacher_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取教师用户用户组
			 */
			async get_group_user_teacher_users() {
							this.form["teacher_users"] = this.$store.state.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=教师用户");
				if(json.result && json.result.obj){
					this.group_user_teacher_users = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_teacher_users(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_teacher_users.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["teacher_users"] = id
									_this.disabledObj['teacher_users' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "teacher_users") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_teacher_users(id){
				var obj = this.list_user_teacher_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
						/**
			 * 上传宣传封面
			 * @param {Object} param 图片参数
			 */
			upload_promotional_cover(param){
									this.uploadFile(param.file, "promotional_cover");
								},
	
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
																if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
																		$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){

																																			

			},

																																																							async submit(param, func){
				if (!param) {
					param = this.form;
				}
								var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																									ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
																																																																																							return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/publicity_and_education/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/publicity_and_education/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/publicity_and_education/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/publicity_and_education/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/publicity_and_education/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
													this.get_list_user_teacher_users();
					this.get_group_user_teacher_users();
									},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




</style>
